﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class mangVTSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public mangVTSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal adByForeach_MVTONSITE(SITE_MANGVT entity)
        {
            dbContext.SITE_MANGVT.Add(entity);
            dbContext.SaveChanges();
            return entity.ID;
        }

        public decimal addorUpdate_MVTONSITE(SITE_MANGVT entity)
        {
            if (entity.ID == 0) dbContext.SITE_MANGVT.Add(entity);
            else
            {
                dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            }

            dbContext.SaveChanges();
            return entity.ID;
        }

        public SITE_MANGVT Get_VNPTSITE_MVT(int siteId, string MangVt)
        {
            try
            {
                return dbContext.SITE_MANGVT.Where(p => p.SITEID == siteId && p.MANGVT == MangVt).First();
            }
            catch (Exception ex){
                SITE_MANGVT _ret = new SITE_MANGVT(); return _ret;
            }
        }

        public SITE_MANGVT Get_VNPTSITE_MVT_Byid(int _Id)
        {
            try
            {
                return dbContext.SITE_MANGVT.Where(p => p.ID == _Id).First();
            }
            catch (Exception ex)
            {
                SITE_MANGVT _ret = new SITE_MANGVT(); return _ret;
            }
        }

        public bool DeteleMVTONSITE(int Id)
        {
            try
            {
                var _entity = Get_VNPTSITE_MVT_Byid(Id);
                dbContext.SITE_MANGVT.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public List<MANGVT> _getList()
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.MANGVT.ToList();
            }
            catch (Exception e)
            {
                List<MANGVT> _listReturn = new List<MANGVT>();
                return _listReturn;
            }
        }

        public List<SITE_MANGVT> _getList_On_Vnpt_Site(int _vnptSiteId)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                //string[] _listMVT =  dbContext.SITE_MANGVT.Where(p => p.SITEID == _vnptSiteId).Select(p => p.MANGVT).ToArray();
                return dbContext.SITE_MANGVT.Where(p => p.SITEID == _vnptSiteId).ToList();
            }
            catch (Exception e)
            {
                List<SITE_MANGVT> _listReturn = new List<SITE_MANGVT>();
                return _listReturn;
            }
        }

        public MANGVT GetByid(string ma)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.MANGVT.Where(p => p.MA == ma).First();
            }
            catch (Exception e)
            {
                MANGVT _listReturn = new MANGVT();
                return _listReturn;
            }
        }

        public bool checkMangVtOnSite(int siteId, string mangvt)
        {
            try
            {
                var _entity = dbContext.SITE_MANGVT.Where(p => p.MANGVT == mangvt && p.SITEID == siteId).First();
                if (_entity != null) return true;
                else return false;
            }
            catch (Exception ex) { return false; }
        }



    }
}
